package com.bluesky.light.healthnews;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 健康新聞在存取數據庫時所用到的功能
 * 
 * @author 
 * 
 */
public class HealthNewsService {

	/**
	 * 管理HealthNewsService 所有方法的對象
	 */
	private static HealthNewsService mgr = null;

	/**
	 * 
	 */
	private HealthNewsService() {
	}

	/**
	 * 在數據庫存取健康新聞資料的對象
	 */
	private HealthNewsRamDAO dao = new HealthNewsRamDAO();

	/**
	 * @return
	 */
	public static HealthNewsService getInstance() {
		if (mgr == null) {
			mgr = new HealthNewsService();
		}
		return mgr;
	}

	/**
	 * 要用到HealthNewsService時, 調用此方法創建一個HealthNewsService的實例
	 * @return
	 */
	public List<HealthNews> getNewses() {
		return dao.getNewses();
	}

	/**
	 * 更新健康新聞
	 */
	public void update() {
		
		HealthNewsSpider spider = new HealthNewsSpider();

		String title = "";
		String content = "";
		String url = "";

		HealthNews news = null;

		dao.delete();

		while (spider.next()) {
			
			if ((title = spider.catchTitle()) != null) {
				news = new HealthNews();
				news.setTitle(title);
			}
			if ((url = spider.catchUrl()) != null) {
				news.setUrl(url);
			}
			if ((content = spider.catchContent()) != null) {
				news.setContent(content);
				news.setUpdateTime(new Date());
				dao.add(news);
			}
		}
	}

	/**
	 * @author
	 * 
	 */
	private class HealthNewsSpider {

		public BufferedReader br = null;

		InputStream urlStream = null;

		public String line = null;

		URL url = null;

		HttpURLConnection httpConn = null;

		/**
		 * 
		 */
		public HealthNewsSpider() {
			try {
				url = new URL("http://www.cn.wsj.com/gb/hea.asp");
				httpConn = (HttpURLConnection) url.openConnection();
				httpConn.connect();
				urlStream = httpConn.getInputStream();

				br = new BufferedReader(new InputStreamReader(urlStream));
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		/**
		 * @return
		 */
		public String catchUrl() {
			Pattern p = Pattern
					.compile("^(<div style=\"padding:8px 0px 0px 0px;).*(</a>)");
			Matcher m = p.matcher(line);
			String title = "";
			try {
				if (m.find()) {
					title = m.group().trim();
				}
			} catch (IllegalStateException e) {
				return null;
			}
			return (title == "" ? null : ("http://www.cn.wsj.com/gb/" + title
					.substring(113, 135)));
		}

		/**
		 * @return
		 */
		private String catchTitle() {
			Pattern p = Pattern
					.compile("^(<div style=\"padding:8px 0px 0px 0px;).*(</a>)");
			Matcher m = p.matcher(line);
			String title = "";
			try {
				if (m.find()) {
					title = m.group().trim();
				}
			} catch (IllegalStateException e) {
				return null;
			}
			return (title == "" ? null : title.substring(151,
					(title.length() - 4)));
		}

		/**
		 * @return
		 */
		private String catchContent() {
			Pattern p = Pattern
					.compile("^(<div style=\"font-size:14px;line-height:160%;).*(<span)");
			Matcher m = p.matcher(line);
			String content = "";
			String contentHead = "";

			try {
				if (m.find()) {
					content = m.group().trim();
					Pattern pHead = Pattern.compile(".*</a>");
					Matcher mHead = pHead.matcher(content);
					if (mHead.find()) {
						contentHead = mHead.group();
					}
				}
			} catch (IllegalStateException e) {
				return null;
			}
			return (content == "" ? null : content.substring(
					contentHead.length(), (content.length() - 5)));
		}

		/**
		 * @return
		 */
		public boolean next() {

			String tmpLine = "";
			try {
				tmpLine = br.readLine();
				if (tmpLine != null) {
					line = new String(tmpLine.getBytes(), "GB2312");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (tmpLine == null) {
				return false;
			}
			return true;
		}
	}

	/**
	 * @return
	 */
	public HealthNews getLastNews() {
		return dao.getLastNews();
	}
}
